using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ReportSubscriptionManager
{
	public partial class Form1 : Form
	{
		//System.Collections.Generic.List<Reportserver.Subscription> ReportSub = new System.Collections.Generic.List<Reportserver.Subscription>();
		public Form1()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			Reportserver.ReportingService2005 rs = new Reportserver.ReportingService2005();
			//rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
			string myDomain = "LocalHost";
			rs.Credentials = new System.Net.NetworkCredential(UserID.Text, Password.Text, myDomain);

			foreach (DataGridViewRow r in dataGridView1.Rows)
			{
				if (Convert.ToBoolean(r.Cells["ReRun"].Value) == true)
				{
					rs.FireEvent("TimedSubscription", Convert.ToString(r.Cells["ReportID"].Value));
					r.Cells["ReRun"].Value = false;
				}
			}
			toolStripStatusLabel1.Text = "Done";            
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			//LoadGrid();
			
			//this.Width = dataGridView1.Columns[0].Width + dataGridView1.Columns[1].Width + dataGridView1.Columns[2].Width + dataGridView1.Columns[3].Width + 50;
		 }

		private void button2_Click(object sender, EventArgs e)
		{
			LoadGrid();
		}
		private void LoadGrid()
		{
			Reportserver.ReportingService2005 rs = new Reportserver.ReportingService2005();			
			toolStripStatusLabel1.Text = "Logging In";
			Application.DoEvents();
			//rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
			string myDomain = "Localhost";
			rs.Credentials = new System.Net.NetworkCredential(UserID.Text, Password.Text, myDomain);			
			toolStripStatusLabel1.Text = "Getting Subscription Data";
			Application.DoEvents();
			Reportserver.Subscription[] subscriptions = rs.ListSubscriptions(null, null);			
			dataGridView1.Rows.Clear();
			foreach (Reportserver.Subscription sub in subscriptions)
			{
				dataGridView1.Rows.Add(null, sub.SubscriptionID, sub.Report, sub.Status, sub.LastExecuted, sub.Path);                
				//ReportSub.Add(sub);
			}
			toolStripStatusLabel1.Text = "";
		}

		private void btnSelectFailed_Click(object sender, EventArgs e)
		{
			String reportstatus;
			foreach (DataGridViewRow r in dataGridView1.Rows)
			{
				if (r.Cells["ReportStatus"].Value != null)
				{
					reportstatus = (string)r.Cells["ReportStatus"].Value;
					if (reportstatus.ToUpper().Contains("FAIL") == true)
					{
						r.Cells["ReRun"].Value = true;
					}
				}
			}
		}

		private void button3_Click(object sender, EventArgs e)
		{
			LoadGrid();
			dataGridView1.AutoResizeColumns();
		}		
	}
}